package org.loofer.uikit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;

public class ViewForeground {
    private Context mContext;
    private Drawable mDrawable;
    private int mDrawableHeight;
    private int mDrawableWidth;
    private boolean mForegroundBoundsChanged;
    private boolean mForegroundInPadding;
    private View mView;

    public ViewForeground(View view) {
        this.mForegroundBoundsChanged = false;
        this.mForegroundInPadding = true;
        this.mContext = view.getContext();
        this.mView = view;
    }

    public ViewForeground(View view, Drawable drawable) {
        this(view);
        setDrawable(drawable);
    }

    public ViewForeground(View view, int resId) {
        this(view);
        setDrawable(resId);
    }

    public void setDrawable(int resId) {
        setDrawable(this.mContext.getResources().getDrawable(resId));
    }

    public void setDrawable(Drawable drawable) {
        if (this.mDrawable != drawable) {
            View view = this.mView;
            int oldWidth = this.mDrawableWidth;
            int oldHeight = this.mDrawableHeight;
            updateDrawable(drawable);
            if (!(oldWidth == this.mDrawableWidth && oldHeight == this.mDrawableHeight)) {
                view.requestLayout();
            }
            view.invalidate();
        }
    }

    public Drawable getDrawable() {
        return this.mDrawable;
    }

    public void setForegroundInPadding(boolean inPadding) {
        if (this.mForegroundInPadding != inPadding) {
            this.mForegroundInPadding = inPadding;
            dispatchBoundsChanged();
        }
    }

    public void dispatchDrawableStateChanged() {
        if (this.mDrawable != null && this.mDrawable.isStateful()) {
            this.mDrawable.setState(this.mView.getDrawableState());
        }
    }

    public void dispatchBoundsChanged() {
        this.mForegroundBoundsChanged = true;
    }

    public void dispatchDraw(Canvas canvas) {
        Drawable foreground = this.mDrawable;
        if (foreground != null) {
            View view = this.mView;
            if (this.mForegroundBoundsChanged) {
                int left;
                int right;
                int top;
                int bottom;
                this.mForegroundBoundsChanged = false;
                int w = view.getWidth();
                int h = view.getHeight();
                if (this.mForegroundInPadding) {
                    left = view.getPaddingLeft();
                    right = w - view.getPaddingRight();
                    top = view.getPaddingTop();
                    bottom = h - view.getPaddingBottom();
                } else {
                    left = 0;
                    right = w;
                    top = 0;
                    bottom = h;
                }
                foreground.setBounds(left, top, right, bottom);
            }
            foreground.draw(canvas);
        }
    }

    private void updateDrawable(Drawable d) {
        View view = this.mView;
        if (this.mDrawable != null) {
            this.mDrawable.setCallback(null);
            view.unscheduleDrawable(this.mDrawable);
        }
        this.mDrawable = d;
        if (d != null) {
            d.setCallback(view);
            if (d.isStateful()) {
                d.setState(view.getDrawableState());
            }
            this.mDrawableWidth = d.getIntrinsicWidth();
            this.mDrawableHeight = d.getIntrinsicHeight();
            return;
        }
        this.mDrawableHeight = -1;
        this.mDrawableWidth = -1;
    }
}
